﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using MOCSoftware.Utilities.JpgMetaDataViewer.Model;
using MOCSoftware.Utilities.JpgMetaDataViewer.Properties;
using MOCSoftware.Utilities.JpgMetaDataViewer.Utility;
using MOCSoftware.Utilities.JpgMetaDataViewer.View;

namespace MOCSoftware.Utilities.JpgMetaDataViewer.Viewmodel
{
    internal class MainWindowViewModel : ViewModelBase
    {
        private const int _PROGRESS_BAR_MINIMUM = 0;
        private const int _PROGRESS_BAR_MAXIMUM = 100;

        private JpgManager _jpgManager;

        public ImageViewModel Image { get; private set; }
        public MetaDataViewModel MetaData { get; private set; }
        public AboutViewModel AboutViewModel { get; private set; }

        internal MainWindowViewModel(JpgManager jpgManager, ImageViewModel imageViewModel, MetaDataViewModel metaDataViewModel, AboutViewModel aboutViewModel)
        {
            _jpgManager = jpgManager;
            Image = imageViewModel;
            MetaData = metaDataViewModel;
            AboutViewModel = aboutViewModel;
        }

        public event EventHandler RequestClose;

        #region ViewModel Binding Properties

        private MainMenuViewModel _mainMenu;
        public MainMenuViewModel MainMenu
        {
            get { return _mainMenu ?? (_mainMenu = new MainMenuViewModel()); }
            set
            {
                _mainMenu = value;
                OnPropertyChanged("MainMenu");
            }
        }

        private Visibility _progressBarVisibility;
        /// <summary>
        /// Flag for displaying or hiding the progress bar
        /// </summary>
        public Visibility ProgressBarVisibility
        {
            get
            {
                return _progressBarVisibility;
            }
            set
            {
                _progressBarVisibility = value;
                OnPropertyChanged("ProgressBarVisibility");
            }
        }

        private Cursor _formCursor;

        public Cursor FormCursor
        {
            get
            {
                return _formCursor;
            }
            set
            {
                _formCursor = value;
                OnPropertyChanged("FormCursor");
            }
        }

        private int _progressBarMinimum;
        public int ProgressBarMinimum
        {
            get
            {
                return _progressBarMinimum;
            }
            set
            {
                _progressBarMinimum = value;
                OnPropertyChanged("ProgressBarMinimum");
            }
        }

        private int _progressBarMaximum;
        public int ProgressBarMaximum
        {
            get
            {
                return _progressBarMaximum;
            }
            set
            {
                _progressBarMaximum = value;
                OnPropertyChanged("ProgressBarMaximum");
            }
        }

        private int _progressBarCurrent;
        public int ProgressBarCurrent
        {
            get
            {
                return _progressBarCurrent;
            }
            set
            {
                _progressBarCurrent = value;
                OnPropertyChanged("ProgressBarCurrent");
            }
        }

        private string _statusMessage;
        /// <summary>
        /// Shows a message in the application status bar
        /// </summary>
        public string StatusMessage
        {
            get
            {
                return _statusMessage;
            }
            set
            {
                _statusMessage = value;
                OnPropertyChanged("StatusMessage");
            }
        }

        private bool _formIsEnabled;
        public bool FormIsEnabled
        {
            get
            {
                return _formIsEnabled;
            }
            set
            {
                _formIsEnabled = value;
                OnPropertyChanged("FormIsEnabled");
            }
        }

        private RelayCommand _closeCommand;
        public ICommand CloseCommand
        {
            get { return _closeCommand ?? (_closeCommand = new RelayCommand(param => OnViewCommand(RequestClose))); }
        }

        private RelayCommand _aboutCommand;
        public ICommand AboutCommand
        {
            get { return _aboutCommand ?? (_aboutCommand = new RelayCommand(param => OnAboutCommand())); }
        }

        private RelayCommand _loadImageFileCommand;
        public ICommand LoadImageFileCommand
        {
            get { return _loadImageFileCommand ?? (_loadImageFileCommand = new RelayCommand(param => OnLoadImageCommand())); }
        }

        #endregion

        internal override void Initialise()
        {
            InitProgress();
            InitViewModelMenu();
            MainMenu.MergeMenus(_menuItemCollection);
            FormIsEnabled = true;
        }

        public void InitProgress()
        {
            ProgressBarMinimum = _PROGRESS_BAR_MINIMUM;
            ProgressBarMaximum = _PROGRESS_BAR_MAXIMUM;
            ProgressBarCurrent = _PROGRESS_BAR_MINIMUM;
            ProgressBarVisibility = Visibility.Hidden;
        }

        private MenuItemCollection _menuItemCollection;

        private void InitViewModelMenu()
        {
            _menuItemCollection = new MenuItemCollection
                                      {
                                          new MenuItemViewModel {Header = "_File"},
                                          new MenuItemViewModel { Header = "L_oad Image File", Parent = "_File", Command = LoadImageFileCommand, Priority = 11},
                                          new MenuItemViewModel
                                              {Header = "E_xit", Parent = "_File", Command = CloseCommand, Priority = 100},
                                          new MenuItemViewModel {Header = "_Help", Priority = 10},
                                          new MenuItemViewModel
                                              {Header = "_About", Parent = "_Help", Command = AboutCommand, Priority  = 10}
                                      };
        }

        internal void UpdateProgress(int value)
        {
            ProgressBarVisibility = Visibility.Visible;

            ProgressBarCurrent = value;
        }

        internal void ShowBusy(bool busy)
        {
            FormCursor = busy ? Cursors.Wait : Cursors.Arrow;
            FormIsEnabled = !busy;
        }

        private void OnAboutCommand()
        {
            AboutViewModel.OpenModal();
        }

        private void OnLoadImageCommand()
        {
            _jpgManager.LoadJpgImage();
        }
    }
}
